package com.superman.view;

import com.superman.entity.Car;
import com.superman.entity.Car_Brand_Category;
import com.superman.entity.User;
import com.superman.util.Constants;
import com.superman.util.GUID;
import com.superman.util.JPanelCarAdmin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AddUI {
    private User user;
    Query query = new Query();
    AddAndAlter addAndAlter = new AddAndAlter();
    JPanelCarAdmin jPanelCar = new JPanelCarAdmin();

    private JFrame frame = new JFrame("管理员添加汽车");
    private Container container = frame.getContentPane();

    //上
    private JPanel upside_panel = new JPanel();
    private JLabel brand_lab = new JLabel("       品牌:");
    private JLabel category_lab = new JLabel("       类型:");
    private JLabel model_lab = new JLabel("       型号:");
    private JLabel car_number_lab = new JLabel("       车牌号:");
    private JLabel t_comments_lab = new JLabel("       概要:");
    private JLabel color_lab = new JLabel("       颜色:");
    private JLabel price_lab = new JLabel("       汽车价格:");
    private JLabel rent_lab = new JLabel("       每日租金:");
    private JLabel status_lab = new JLabel("       是否可借:");
    private JLabel useable_lab = new JLabel("       是否上架:");
    private JComboBox brand_cb = new JComboBox(query.queryBrand());
    private JComboBox category_cb = new JComboBox(query.queryCategory());
    private JTextField model_tf = new JTextField(8);
    private JTextField car_number_tf = new JTextField(8);
    private JTextField t_comments_tf = new JTextField(8);
    private JTextField color_tf = new JTextField(8);
    private JTextField price_tf = new JTextField(8);
    private JTextField rent_tf = new JTextField(8);
    private JComboBox status_cb = new JComboBox(Constants.status);
    private JComboBox useable_cb = new JComboBox(Constants.useable);


    //中
    private JPanel middle_panel = new JPanel();
    private JButton add_btn = new JButton("确认添加");
    private JButton backup_btn = new JButton("返回");
    private JButton quit_btn = new JButton("退出");
    private JLabel result_lab = new JLabel("二嗨租车添加工作进行中...");

    //下
    private JPanel downside_panel = new JPanel();
    private JPanel add_panel;

    public AddUI(User user){
        frame.setSize(1000,500);
        container.setLayout(new BorderLayout());
        initFrame(user);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void initFrame(User user){
        //上侧
        upside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20,20));
        upside_panel.add(brand_lab);
        upside_panel.add(brand_cb);
        upside_panel.add(category_lab);
        upside_panel.add(category_cb);
        upside_panel.add(model_lab);
        upside_panel.add(model_tf);
        upside_panel.add(car_number_lab);
        upside_panel.add(car_number_tf);
        upside_panel.add(t_comments_lab);
        upside_panel.add(t_comments_tf);
        upside_panel.add(color_lab);
        upside_panel.add(color_tf);
        upside_panel.add(price_lab);
        upside_panel.add(price_tf);
        upside_panel.add(rent_lab);
        upside_panel.add(rent_tf);
        upside_panel.add(status_lab);
        upside_panel.add(status_cb);
        upside_panel.add(useable_lab);
        upside_panel.add(useable_cb);
        container.add(upside_panel, "North");

        //中部
        CardLayout cardLayout = new CardLayout(20, 20);//设置布局管理器为卡片式
        middle_panel.setLayout(cardLayout);
        container.add(middle_panel, "Center");

        //下侧
        downside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
        downside_panel.add(add_btn);
        downside_panel.add(backup_btn);
        downside_panel.add(quit_btn);
        downside_panel.add(result_lab);
        container.add(downside_panel, "South");

        add_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String brand = String.valueOf(brand_cb.getSelectedItem());
                String brand_id = query.getBrandID(brand);
                String category = String.valueOf(category_cb.getSelectedItem());
                String category_id = query.getCategoryID(category);
                String model = model_tf.getText();
                String car_number = car_number_tf.getText();
                String t_comments = t_comments_tf.getText();
                String color = color_tf.getText();
                double price = Double.parseDouble(price_tf.getText());
                double rent = Double.parseDouble(rent_tf.getText());
                int status = (status_cb.getSelectedItem().equals("可借")? 0:1);
                int useable = (useable_cb.getSelectedItem().equals("上架")? 0:1);
                Car car = new Car(GUID.getGUID(), car_number, brand_id, model, color, category_id, t_comments, price, rent, status, useable);
                List<Car_Brand_Category> list = addAndAlter.addCar(car);
                if(list.isEmpty()){
                    result_lab.setText("添加失败啦！自己找找原因");
                }else {
                    result_lab.setText("添加成功！");
                    add_panel = jPanelCar.getJPanelCar(list);
                    downside_panel.add(add_panel);
                }
            }
        });

        backup_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new AdminUI(user);
            }
        });

        quit_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new LoginUI();
            }
        });
    }
    public void hide(){
        frame.setVisible(false);
    }

}
